home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / PRINTMSG.C < prev    next >
C/C++ Source or Header  |  1991-11-21  |  5KB  |  155 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    p r i n t m s g . c                                             */
  3. /*                                                                    */
  4. /*    Support routines for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Changes Copyright 1990, 1991 (c) Andrew H. Derbyshire           */
  7. /*                                                                    */
  8. /*    History:                                                        */
  9. /*       21Nov1991 Break out of lib.c                          ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17.  
  18. #ifdef __CORE__
  19. #define __HEAPCHECK__
  20. #endif
  21.  
  22. #ifdef __HEAPCHECK__
  23. #include <alloc.h>
  24. #else
  25. #ifdef __CORELEFT__
  26. #include <alloc.h>
  27. #endif
  28. #endif
  29.  
  30. /*--------------------------------------------------------------------*/
  31. /*                    UUPC/extended include files                     */
  32. /*--------------------------------------------------------------------*/
  33.  
  34. #include "lib.h"
  35. #include "dater.h"
  36.  
  37. /*--------------------------------------------------------------------*/
  38. /*                          Global variables                          */
  39. /*--------------------------------------------------------------------*/
  40.  
  41. #ifdef __HEAPCHECK__
  42. currentfile();
  43. #endif
  44.  
  45. int debuglevel = 1;
  46. boolean logecho = FALSE;
  47. FILE *logfile = stdout;
  48.  
  49. #ifdef __CORE__
  50. long  *lowcore = NULL;
  51. char  *copyright = (char *) 4;
  52. char  *copywrong = NULL;
  53. #endif
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*   p r i n t m s g                                                  */
  57. /*                                                                    */
  58. /*   Print an error message if its severity level is high enough.     */
  59. /*   Print message on standard output if not in remote mode           */
  60. /*   (call-in).  Always log the error message into the log file.      */
  61. /*                                                                    */
  62. /*   Modified by ahd 10/01/89 to check for Turbo C NULL pointers      */
  63. /*   being de-referenced anywhere in program.  Fixed 12/14/89         */
  64. /*                                                                    */
  65. /*   Modified by ahd 04/18/91 to use true variable parameter list,    */
  66. /*   supplied by Harald Boegeholz                                     */
  67. /*--------------------------------------------------------------------*/
  68.  
  69. void printmsg(int level, char *fmt, ...)
  70. {
  71.    va_list arg_ptr;
  72.  
  73. #ifdef __CORELEFT__
  74.    static unsigned freecore = 63 * 1024;
  75.    unsigned nowfree;
  76. #endif
  77.  
  78. #ifdef __HEAPCHECK__
  79.    static boolean recurse = FALSE;
  80.    int heapstatus;
  81.  
  82.    heapstatus = heapcheck();
  83.    if (heapstatus == _HEAPCORRUPT)
  84.    {
  85.       debuglevel = level;  /* Force this last message to print ahd   */
  86.       printf("\a*** HEAP IS CORRUPTED ***\a\n");
  87.    }
  88.  
  89. #ifdef __CORE__
  90.    if (*lowcore != 0L)
  91.    {
  92.       putchar('\a');
  93.       debuglevel = level;  /* Force this last message to print ahd   */
  94.    }
  95. #endif
  96.  
  97. #endif
  98.  
  99. #ifdef __CORELEFT__
  100.    nowfree = coreleft();
  101.    if (nowfree < freecore)
  102.    {
  103.       freecore = (nowfree / 10) * 9;
  104.       printmsg(0,"Free memory = %u bytes", nowfree);
  105.    }
  106. #endif
  107.  
  108.    if (level <= debuglevel)
  109.    {
  110.       va_start(arg_ptr,fmt);
  111.  
  112.       if (logfile != stdout)
  113.       {
  114.          if (logecho || (debuglevel > 1))
  115.          {
  116.             vfprintf(stderr, fmt, arg_ptr);
  117.             fputc('\n',stderr);
  118.          }
  119.          if ( debuglevel > 1 )
  120.             fprintf(logfile, "(%d) ", level);
  121.          else
  122.             fprintf(logfile, "%s ", dater( time( NULL ), NULL));
  123.       } /* if (logfile != stdout) */
  124.  
  125.       if (!ferror(logfile))
  126.          vfprintf(logfile, fmt, arg_ptr);
  127.       if (!ferror(logfile))
  128.          fputc('\n',logfile);
  129.       if ( level * 2 < debuglevel )
  130.          fflush( logfile );
  131.       if (ferror(logfile))
  132.       {
  133.          perror(LOGFILE);
  134.          abort();
  135.       } /* if */
  136.  
  137. #ifdef __HEAPCHECK__
  138.       if ( !recurse )
  139.       {
  140.          recurse = TRUE;
  141. #ifdef __CORE__
  142.          if (*lowcore != 0L)
  143.             panic();
  144.          if (!equal(copyright,copywrong))
  145.             panic();
  146. #endif
  147.          if (heapstatus == _HEAPCORRUPT)
  148.             panic();
  149.          recurse = FALSE;
  150.       }
  151. #endif
  152.    } /* if (level <= debuglevel) */
  153.  
  154. } /*printmsg*/
  155.